﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.SqlServerCe;
using System.Data;
using MahApps.Metro;
using MahApps.Metro.Controls;
using Awesomium.Core;

namespace AnimeCode
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : MetroWindow
    {
        private void generateMenuBar(string page)
        {
            string src = "document.getElementById('mainmenu').innerHTML = '" +
                         "<ul id=\"navigation\">";

            //Menu Index
            if (page == "index")
                src += "<li class=\"current_page_item\"><a name=\"index\" onclick=\"Closing(\\'index.html\\'); configurator(this);\" href=\"#\">Home</a></li>";
            else
                src += "<li><a name=\"index\" onclick=\"Closing(\\'index.html\\'); configurator(this);\" href=\"#\">Home</a></li>";
            //-----//

            //Menu Archive
            if (page == "archive")
                src += "<li class=\"current_page_item\"><a href=\"#\">Achieve</a>";
            else
                src += "<li><a href=\"#\">Achieve</a>";

            src += "<ul>" +
                      "<li><a name=\"collection\" onclick=\"Closing(\\'archives.html\\'); configurator(this);\" href=\"#\">Collection List</a></li>"+
                      "<li><a name=\"watch\" onclick=\"Closing(\\'watchlist.html\\'); configurator(this);\" href=\"#\">Watch List</a></li>"+
                   "</ul>" +
                   "</li>";

            //Menu Top List
            if (page == "hof")
                src += "<li class=\"current_page_item\"><a name=\"hof\" onclick=\"Closing(\\'hof.html\\'); configurator(this);\" href=\"#\">Top List</a></li>";
            else
                src += "<li><a name=\"hof\" onclick=\"Closing(\\'hof.html\\'); configurator(this);\" href=\"#\">Top List</a></li>";
            //------//

            //Menu Scout
            if (page == "scout")
                src += "<li class=\"current_page_item\"><a name=\"scout\" onclick=\"Closing(\\'scout.html\\'); configurator(this);\" href=\"#\">Scout</a></li>";
            else
                src += "<li><a name=\"scout\" onclick=\"Closing(\\'scout.html\\'); configurator(this);\" href=\"#\">Scout</a></li>";
            //-----//

            src+="<li class=\"last\"><a href=\"#\">About</a></li>" +
                 "</ul>';";

            var script = src;
            web_Control.ExecuteJavascript(script);
        }

        private void homeSetup()
        {
            SqlCeConnection conn = new SqlCeConnection();
            conn.ConnectionString = GlobalVar.connection_string;

            SqlCeCommand cmd;
            SqlCeDataReader dt_reader;
            String sql = null;

            String src = String.Empty;
            var script = src;

            //Anime of The Month
            try
            {
                conn.Open();
                sql = "SELECT a.title, a.type, b.box, c.overall FROM ((Anime_Information a INNER JOIN Anime_Image b ON a.id=b.id) INNER JOIN Anime_Rating c ON a.id=c.id) WHERE a.title = '" + Module.loadVal("anime_of_the_month") + "';";
                cmd = new SqlCeCommand(sql, conn);
                dt_reader = cmd.ExecuteReader();
                while (dt_reader.Read())
                {
                    src = "document.getElementById('aotm_title').innerHTML = '" + dt_reader.GetValue(0).ToString() + "';" +
                          "document.getElementById('aotm_type').innerHTML = '" + dt_reader.GetValue(1).ToString() + "';" +
                          "document.getElementById('aotm_box').src = '" + GlobalVar.image_path + "box/" + dt_reader.GetValue(2).ToString() + "';" +
                          "document.getElementById('aotm_rating').innerHTML = '" +
                          "<div class=\"myrating_star_user\">" +
                          "   <div style=\"width:" + (Convert.ToInt16(dt_reader.GetValue(3)) * 10) + "%\">&nbsp;</div>" +
                          "</div> " + Convert.ToString(dt_reader.GetValue(3)) + " / 10';";
                }
                script = src;
                web_Control.ExecuteJavascript(script);
                conn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            //SlideShow
            try
            {
                int counter = 0;
                src = String.Empty;

                conn.Open();
                sql = "SELECT TOP(5) a.title, a.synopsis, b.slide_pic1 FROM Anime_Information a INNER JOIN Anime_Image b ON a.id=b.id ORDER BY a.id DESC;";
                cmd = new SqlCeCommand(sql, conn);
                dt_reader = cmd.ExecuteReader();
                while (dt_reader.Read())
                {
                    src += "document.getElementById('slide" + counter.ToString() + "_pic').src = '" + GlobalVar.image_path + "slide/" + dt_reader.GetValue(2).ToString() + "';" +
                           "document.getElementById('slide" + counter.ToString() + "_title').innerHTML = '" + dt_reader.GetValue(0).ToString() + "';";

                    if (Convert.ToString(dt_reader.GetValue(1)).Length > 210)
                        src += "document.getElementById('slide" + counter.ToString() + "_synopsis').innerHTML = '" + Convert.ToString(dt_reader.GetValue(1)).Substring(0, 210) + "...';";
                    else
                        src += "document.getElementById('slide" + counter.ToString() + "_synopsis').innerHTML = '" + Convert.ToString(dt_reader.GetValue(1)) + "';";
                    
                    counter += 1;
                }
                script = src;
                web_Control.ExecuteJavascript(script);
                conn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            //Stats
            try
            {
                conn.Open();
                sql = "SELECT COUNT(*) FROM Anime_Information;";
                cmd = new SqlCeCommand(sql, conn);
                dt_reader = cmd.ExecuteReader();
                while (dt_reader.Read())
                    src = "document.getElementById('number_collection').innerHTML = '" + dt_reader.GetValue(0).ToString() + "';";

                sql = "SELECT COUNT(*) FROM Anime_Stock;";
                cmd = new SqlCeCommand(sql, conn);
                dt_reader = cmd.ExecuteReader();
                while (dt_reader.Read())
                    src += "document.getElementById('number_watch').innerHTML = '" + dt_reader.GetValue(0).ToString() + "';";

                sql = "SELECT COUNT(*) FROM Anime_Scout;";
                cmd = new SqlCeCommand(sql, conn);
                dt_reader = cmd.ExecuteReader();
                while (dt_reader.Read())
                    src += "document.getElementById('number_scout').innerHTML = '" + dt_reader.GetValue(0).ToString() + "';";

                script = src;
                web_Control.ExecuteJavascript(script);
                conn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            //Random Anime
            try
            {
                int counter = 0;
                src = String.Empty;

                conn.Open();
                sql = "SELECT TOP(5) a.title, a.synopsis, b.icon FROM Anime_Information a INNER JOIN Anime_Image b ON a.id=b.id ORDER BY NEWID();";
                cmd = new SqlCeCommand(sql, conn);
                dt_reader = cmd.ExecuteReader();
                while (dt_reader.Read())
                {
                    src += "document.getElementById('sc" + counter.ToString() + "_pic').src = '" + GlobalVar.image_path + "icon/" + dt_reader.GetValue(2).ToString() + "';";

                    if (Convert.ToString(dt_reader.GetValue(0)).Length > 35)
                        src += "document.getElementById('sc" + counter.ToString() + "_title').innerHTML = '" + Convert.ToString(dt_reader.GetValue(0)).Substring(0, 35) + "';";
                    else
                        src += "document.getElementById('sc" + counter.ToString() + "_title').innerHTML = '" + Convert.ToString(dt_reader.GetValue(0)) + "';";

                    if (Convert.ToString(dt_reader.GetValue(1)).Length > 60)
                        src += "document.getElementById('sc" + counter.ToString() + "_synopsis').innerHTML = '" + Convert.ToString(dt_reader.GetValue(1)).Substring(0, 60) + "...';";
                    else
                        src += "document.getElementById('sc" + counter.ToString() + "_synopsis').innerHTML = '" + Convert.ToString(dt_reader.GetValue(1)) + "';";

                    counter += 1;
                }

                script = src;
                web_Control.ExecuteJavascript(script);
                conn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            //Top Anime
            try
            {
                src = String.Empty;

                conn.Open();
                sql = "SELECT TOP(3) a.title, a.synopsis, b.icon FROM (Anime_Information a INNER JOIN Anime_Image b ON a.id=b.id) INNER JOIN Anime_Rating c ON a.id=c.id ORDER BY c.overall DESC;";
                cmd = new SqlCeCommand(sql, conn);
                dt_reader = cmd.ExecuteReader();

                src += "document.getElementById('top_content').innerHTML = '";
                while (dt_reader.Read())
                {
                    src += "<div style=\"margin-top:5px;\">" +
                           "	<div class=\"d_td\" style=\"padding-right:5px;\">" +
                           "		<img src=\"" + GlobalVar.image_path + "icon/" + dt_reader.GetValue(2).ToString() + "\" class=\"mybordered\">" +
                           "    </div>" +
                           "	<div class=\"d_td\" style=\"vertical-align:top;\">" +
                           "		<div style=\"font-size:11px;\"><a href=\"#\">" + dt_reader.GetValue(0).ToString() + "</a></div>";
                    if (Convert.ToString(dt_reader.GetValue(1)).Length > 55)
                        src += "<div style=\"font-size:11px;\">" + Convert.ToString(dt_reader.GetValue(1)).Substring(0, 55) + "</div>";
                    else
                        src += "<div style=\"font-size:11px;\">" + Convert.ToString(dt_reader.GetValue(1)) + "</div>";

                    src += "	</div></div>";
                }

                src += "';";
                script = src;
                web_Control.ExecuteJavascript(script);
                conn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void archiveSetup()
        {
            SqlCeConnection conn = new SqlCeConnection();
            conn.ConnectionString = GlobalVar.connection_string;

            SqlCeCommand cmd;
            SqlCeDataReader dt_reader;
            String sql = null;

            String src = String.Empty;
            var script = src;

            //Anime List
            try
            {
                conn.Open();
                src = "document.getElementById('anime_list').innerHTML = '";
                for (char ch = 'a'; ch <= 'z'; ch++)
                {
                    if (ch == 'a')
                        src += "<div class=\"mod-inner\" style=\"margin:3px 5px 10px 10px;\">";
                    else
                        src += "<div class=\"mod-inner\" style=\"margin:10px 5px 10px 10px;\">";

                    src += "<a name=\"" + Convert.ToString(ch).ToUpper() + "\"></a>";

                    sql = "SELECT id, title FROM Anime_Information WHERE title LIKE '" + Convert.ToString(ch) + "%' ORDER BY title ASC;";
                    cmd = new SqlCeCommand(sql, conn);
                    dt_reader = cmd.ExecuteReader();
                    while (dt_reader.Read())
                        src += "<a href=\"#\" name=\"" + dt_reader.GetValue(0).ToString() + "\" onclick=\"Closing(\\'anime.html\\'); configurator(this);\">" + dt_reader.GetValue(1).ToString() + "</a><br/>";

                    src += "</div>";
                }
                src += "';";

                script = src;
                web_Control.ExecuteJavascript(script);
                conn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            //Top Rated
            try
            {
                conn.Open();
                src = "document.getElementById('top_list').innerHTML = '<ul style=\"margin:10px;\">";

                sql = "SELECT TOP(5) title FROM Anime_Rating ORDER BY overall DESC;";
                cmd = new SqlCeCommand(sql, conn);
                dt_reader = cmd.ExecuteReader();
                while (dt_reader.Read())
                    src += "<li><a href=\"#\">" + dt_reader.GetValue(0).ToString() + "</a></li>";

                src += "</ul>';";
                script = src;
                web_Control.ExecuteJavascript(script);
                conn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            //Latest Updated
            try
            {
                conn.Open();
                src = "document.getElementById('updated_list').innerHTML = '<ul style=\"margin:10px;\">";

                sql = "SELECT TOP(5) title FROM Anime_Information ORDER BY id DESC;";
                cmd = new SqlCeCommand(sql, conn);
                dt_reader = cmd.ExecuteReader();
                while (dt_reader.Read())
                    src += "<li><a href=\"#\">" + dt_reader.GetValue(0).ToString() + "</a></li>";

                src += "</ul>';";
                script = src;
                web_Control.ExecuteJavascript(script);
                conn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            //Most Popular
            try
            {
                conn.Open();
                src = "document.getElementById('popular_list').innerHTML = '<ul style=\"margin:10px;\">";

                sql = "SELECT TOP(5) title FROM Anime_Information ORDER BY hit DESC;";
                cmd = new SqlCeCommand(sql, conn);
                dt_reader = cmd.ExecuteReader();
                while (dt_reader.Read())
                    src += "<li><a href=\"#\">" + dt_reader.GetValue(0).ToString() + "</a></li>";

                src += "</ul>';";
                script = src;
                web_Control.ExecuteJavascript(script);
                conn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void animeSetup(string id)
        {
            SqlCeConnection conn = new SqlCeConnection();
            conn.ConnectionString = GlobalVar.connection_string;

            SqlCeCommand cmd;
            SqlCeDataReader dt_reader;
            String sql = null;

            String src = String.Empty;
            var script = src;

            try
            {
                conn.Open();
                sql = "SELECT a.title, a.main_title, a.alternative_title, a.official_title, a.synopsis, a.type, a.aired_date, a.producer, a.genre, a.category, a.opening_theme, a.ending_theme, a.fansubs, b.box, b.slide_pic1, c.story, c.animation, c.sound, c.character, c.enjoyment, c.overall FROM ((Anime_Information a INNER JOIN Anime_Image b ON a.id=b.id) INNER JOIN Anime_Rating c ON a.id=c.id) WHERE a.id = " + id;
                cmd = new SqlCeCommand(sql, conn);
                dt_reader = cmd.ExecuteReader();
                while (dt_reader.Read())
                {
                    src = "document.getElementById('ac_title').innerHTML = '" + Convert.ToString(dt_reader.GetValue(0)) + "';" +
                          "document.getElementById('ac_main_title').innerHTML = '" + Convert.ToString(dt_reader.GetValue(1)) + "';" +
                          "document.getElementById('ac_alternative_title').innerHTML = '" + Convert.ToString(dt_reader.GetValue(2)) + "';" +
                          "document.getElementById('ac_official_title').innerHTML = '" + Convert.ToString(dt_reader.GetValue(3)) + "';" +
                          "document.getElementById('ac_synopsis').innerHTML = '" + Convert.ToString(dt_reader.GetValue(4)) + "';" +
                          "document.getElementById('ac_type').innerHTML = '" + Convert.ToString(dt_reader.GetValue(5)) + "';" +
                          "document.getElementById('ac_aired_date').innerHTML = '" + Convert.ToString(dt_reader.GetValue(6)) + "';" +
                          "document.getElementById('ac_producer').innerHTML = '" + Convert.ToString(dt_reader.GetValue(7)) + "';" +
                          "document.getElementById('ac_genre').innerHTML = '" + Convert.ToString(dt_reader.GetValue(8)) + "';" +
                          "document.getElementById('ac_category').innerHTML = '" + Convert.ToString(dt_reader.GetValue(9)) + "';" +
                          "document.getElementById('ac_opening_theme').innerHTML = '" + Convert.ToString(dt_reader.GetValue(10)).Replace(Environment.NewLine, "<br/>") + "';" +
                          "document.getElementById('ac_ending_theme').innerHTML = '" + Convert.ToString(dt_reader.GetValue(11)).Replace(Environment.NewLine, "<br/>") + "';" +
                          "document.getElementById('ac_fansubs').innerHTML = '" + Convert.ToString(dt_reader.GetValue(12)).Replace("|", "<br/>") + "';" +
                          "document.getElementById('ac_box').src = '" + GlobalVar.image_path + "box/" + Convert.ToString(dt_reader.GetValue(13)) + "';" +
                          "document.getElementById('ac_slide').src = '" + GlobalVar.image_path + "slide/" + Convert.ToString(dt_reader.GetValue(14)) + "';" +
                          "document.getElementById('ac_story').innerHTML = '<span class=\"myfiller\" style=\"width:" + (Convert.ToInt16(dt_reader.GetValue(15)) * 10).ToString() + "%;\">" + Convert.ToString(dt_reader.GetValue(15)) + "</span>';" +
                          "document.getElementById('ac_animation').innerHTML = '<span class=\"myfiller\" style=\"width:" + (Convert.ToInt16(dt_reader.GetValue(16)) * 10).ToString() + "%;\">" + Convert.ToString(dt_reader.GetValue(16)) + "</span>';" +
                          "document.getElementById('ac_sound').innerHTML = '<span class=\"myfiller\" style=\"width:" + (Convert.ToInt16(dt_reader.GetValue(17)) * 10).ToString() + "%;\">" + Convert.ToString(dt_reader.GetValue(17)) + "</span>';" +
                          "document.getElementById('ac_char').innerHTML = '<span class=\"myfiller\" style=\"width:" + (Convert.ToInt16(dt_reader.GetValue(18)) * 10).ToString() + "%;\">" + Convert.ToString(dt_reader.GetValue(18)) + "</span>';" +
                          "document.getElementById('ac_enjoyment').innerHTML = '<span class=\"myfiller\" style=\"width:" + (Convert.ToInt16(dt_reader.GetValue(19)) * 10).ToString() + "%;\">" + Convert.ToString(dt_reader.GetValue(19)) + "</span>';" +
                          "document.getElementById('ac_overall').innerHTML = '<span class=\"mylastfiller\" style=\"width:" + (Convert.ToInt16(dt_reader.GetValue(20)) * 10).ToString() + "%;\">" + Convert.ToString(dt_reader.GetValue(20)) + "</span>';";
                }
                conn.Close();
                script = src;
                web_Control.ExecuteJavascript(script); 
            }
            catch (SqlCeException ex)
            {
                MessageBox.Show(ex.Message);
            }

            //Episode List
            src = "document.getElementById('ac_episode').innerHTML = '" +
                  "<table class=\"mydata-table\">" +
                  "<tr>" +
                  "   <th>EP</th> <th>Title</th> <th>Duration</th> <th>Air Date</th>" +
                  "</tr>";
            try
            {
                conn.Open();
                sql = "SELECT ep, ep_title, kanji, romaji, duration, aired_date FROM Anime_Episode WHERE id = " + id + " ORDER BY id, len(ep), ep";
                cmd = new SqlCeCommand(sql, conn);
                dt_reader = cmd.ExecuteReader();
                while (dt_reader.Read())
                {
                    src += "<tr>" +
                           "   <td class=\"myfield-right\">" + dt_reader.GetValue(0).ToString() + "</td>" +
                           "   <td><span title=\"" + dt_reader.GetValue(2).ToString() + " (" + dt_reader.GetValue(3).ToString() + ")\">" + dt_reader.GetValue(1).ToString() + "</span></td>" +
                           "   <td class=\"myfield-center\">" + dt_reader.GetValue(4).ToString() + "</td>   <td class=\"myfield-center\">" + dt_reader.GetValue(5).ToString() + "</td>" +
                           "</tr>";
                }
                conn.Close();
                src += "</table>';";
                script = src;
                web_Control.ExecuteJavascript(script);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void watchlistSetup()
        {
            SqlCeConnection conn = new SqlCeConnection();
            conn.ConnectionString = GlobalVar.connection_string;

            SqlCeCommand cmd;
            SqlCeDataReader dt_reader;
            String sql = null;

            String src = String.Empty;
            var script = src;
            int counter = 0;
            
            //SlideShow
            try
            {
                conn.Open();
                sql = "SELECT TOP(3) title, cover, synopsis FROM Anime_Stock ORDER BY NEWID()";
                cmd = new SqlCeCommand(sql, conn);
                dt_reader = cmd.ExecuteReader();
                while (dt_reader.Read())
                {
                    src += "document.getElementById('watch" + Convert.ToString(counter) + "_title').innerHTML = '" + Convert.ToString(dt_reader.GetValue(0)) + "';" +
                           "document.getElementById('watch" + Convert.ToString(counter) + "_pic').src = '" + GlobalVar.image_path + "cover/" + Convert.ToString(dt_reader.GetValue(1)) + "';";

                    if (Convert.ToString(dt_reader.GetValue(2)).Length > 200)
                        src += "document.getElementById('watch" + Convert.ToString(counter) + "_synopsis').innerHTML = '" + Convert.ToString(dt_reader.GetValue(2)).Substring(0, 200).Replace("'", "\\'") + "...';";
                    else
                        src += "document.getElementById('watch" + Convert.ToString(counter) + "_synopsis').innerHTML = '" + Convert.ToString(dt_reader.GetValue(2)).Replace("'", "\\'") + "';";

                    counter += 1;
                }
                conn.Close();
                script = src;
                web_Control.ExecuteJavascript(script);
            }
            catch (SqlCeException ex)
            {
                MessageBox.Show(ex.Message);
            }

            //Stock List
            src = "document.getElementById('watch_list').innerHTML = '<div class=\"d_tr\">";
            counter = 0;

            try
            {
                conn.Open();
                sql = "SELECT title, cover, synopsis, type FROM Anime_Stock ORDER BY date_created DESC";
                cmd = new SqlCeCommand(sql, conn);
                dt_reader = cmd.ExecuteReader();
                while (dt_reader.Read())
                {
                    if (counter % 4 == 0 && counter != 0)
                        src += "</div><div class=\"d_tr\">";

                    src += "<div class=\"d_td\">" +
                           "   <div class=\"anibox\">" +
                           "      <img src=\"" + GlobalVar.image_path + "cover/" + Convert.ToString(dt_reader.GetValue(1)) + "\" class=\"mybordered watchlistimg\"/>" +
                           "      <div class=\"anibox_synopsis\">";

                    if (Convert.ToString(dt_reader.GetValue(0)).Length > 20)
                        src += "<div class=\"anibox_title\">" + Convert.ToString(dt_reader.GetValue(0)).Substring(0, 20) + "</div>";
                    else
                        src += "<div class=\"anibox_title\">" + Convert.ToString(dt_reader.GetValue(0)) + "</div>";
                    
                    if (Convert.ToString(dt_reader.GetValue(2)).Length > 85)
                        src += Convert.ToString(dt_reader.GetValue(2)).Substring(0, 85).Replace("'", "\\'") + "...";
                    else
                        src += Convert.ToString(dt_reader.GetValue(2)).Replace("'", "\\'");

                    src += "      </div>" +
                           "      <div class=\"clr\"></div>" +
                           "      <div class=\"anibox_type\">" + Convert.ToString(dt_reader.GetValue(3)) + "</div>" +
                           "   </div>" +
                           "</div>";

                    counter += 1;
                }
                conn.Close();
                src += "</div>';";
                script = src;
                web_Control.ExecuteJavascript(script);
            }
            catch (SqlCeException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void topSetup()
        {
            SqlCeConnection conn = new SqlCeConnection();
            conn.ConnectionString = GlobalVar.connection_string;

            SqlCeCommand cmd;
            SqlCeDataReader dt_reader;
            String sql = null;

            String src_perfect = String.Empty, 
                   src_excellent = String.Empty, 
                   src_better = String.Empty, 
                   src_good = String.Empty, 
                   src_moderate = String.Empty, 
                   src_fair = String.Empty, 
                   src_bad = String.Empty, 
                   src_poor = String.Empty, 
                   src_terrible = String.Empty;
            
            int c_perfect = 0, 
                c_excellent = 0, 
                c_better = 0, 
                c_good = 0, 
                c_moderate = 0, 
                c_fair = 0, 
                c_bad = 0, 
                c_poor = 0, 
                c_terrible = 0;

            src_perfect = "document.getElementById('hof_perfect').innerHTML = '<center>";
            src_excellent = "document.getElementById('hof_excellent').innerHTML = '<center>";
            src_better = "document.getElementById('hof_better').innerHTML = '<center>";
            src_good = "document.getElementById('hof_good').innerHTML = '<center>";
            src_moderate = "document.getElementById('hof_moderate').innerHTML = '<center>";
            src_fair = "document.getElementById('hof_fair').innerHTML = '<center>";
            src_bad = "document.getElementById('hof_bad').innerHTML = '<center>";
            src_poor = "document.getElementById('hof_poor').innerHTML = '<center>";
            src_terrible = "document.getElementById('hof_terrible').innerHTML = '<center>";

            try
            {
                conn.Open();
                sql = "SELECT a.title, a.overall, b.cover FROM Anime_Rating a INNER JOIN Anime_Image b ON a.id = b.id ORDER BY a.overall DESC";
                cmd = new SqlCeCommand(sql, conn);
                dt_reader = cmd.ExecuteReader();
                while (dt_reader.Read())
                {
                    if (Convert.ToDecimal(dt_reader.GetValue(1)) == Convert.ToDecimal(10))
                    {
                        src_perfect += "<div class=\"d_td\" style=\"padding:5px;\">" +
                                           "<a href=\"#\" onClick=\"return hs.htmlExpand(this)\">" +
                                               "<img src=\"" + GlobalVar.image_path + "Cover Thumb/" + dt_reader.GetValue(2).ToString() + "\" class=\"mybordered\"/>" +
                                           "</a>" +
                                           "<div class=\"listscore2\"><span>Score: " + dt_reader.GetValue(1).ToString() + "</span></div>" +
                                           "<div class=\"highslide-maincontent\">" +
                                               "<img src=\"" + GlobalVar.image_path + "Cover/" + dt_reader.GetValue(2).ToString() + "\" width=\"150\"/>" +
                                           "</div>" +
                                       "</div>";
                        c_perfect += 1;
                    }
                    else if (Convert.ToDecimal(dt_reader.GetValue(1)) >= Convert.ToDecimal(9.5) && Convert.ToDecimal(dt_reader.GetValue(1)) <= Convert.ToDecimal(9.99))
                    {
                        src_excellent += "<div class=\"d_td\" style=\"padding:5px;\">" +
                                               "<a href=\"#\" onClick=\"return hs.htmlExpand(this)\">" +
                                                   "<img src=\"" + GlobalVar.image_path + "Cover Thumb/" + dt_reader.GetValue(2).ToString() + "\" class=\"mybordered\"/>" +
                                               "</a>" +
                                               "<div class=\"listscore2\"><span>Score: " + dt_reader.GetValue(1).ToString() + "</span></div>" +
                                               "<div class=\"highslide-maincontent\">" +
                                                   "<img src=\"" + GlobalVar.image_path + "Cover/" + dt_reader.GetValue(2).ToString() + "\" width=\"150\"/>" +
                                               "</div>" +
                                           "</div>";
                        c_excellent += 1;
                    }
                    
                }
                conn.Close();

                src_perfect += "</center>';";
                src_excellent += "</center>';";
                src_better += "</center>';";
                src_good += "</center>';";
                src_moderate += "</center>';";
                src_fair += "</center>';";
                src_bad += "</center>';";
                src_poor += "</center>';";
                src_terrible += "</center>';";

                var script = src_perfect;
                web_Control.ExecuteJavascript(script);
                script = src_excellent;
                web_Control.ExecuteJavascript(script);
                script = src_better;
                web_Control.ExecuteJavascript(script);
                script = src_good;
                web_Control.ExecuteJavascript(script);
                script = src_moderate;
                web_Control.ExecuteJavascript(script);
                script = src_fair;
                web_Control.ExecuteJavascript(script);
                script = src_bad;
                web_Control.ExecuteJavascript(script);
                script = src_poor;
                web_Control.ExecuteJavascript(script);
                script = src_terrible;
                web_Control.ExecuteJavascript(script);
            }
            catch (SqlCeException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void scoutSetup()
        {
            SqlCeConnection conn = new SqlCeConnection();
            conn.ConnectionString = GlobalVar.connection_string;

            SqlCeCommand cmd;
            SqlCeDataReader dt_reader;
            String sql = null;

            String src = String.Empty;
            var script = src;
            int counter = 0;

            src = "document.getElementById('scout_unsorted').innerHTML = '<div class=\"d_tr\"><center>";
            try
            {
                conn.Open();
                sql = "SELECT * FROM Anime_Scout";
                cmd = new SqlCeCommand(sql, conn);
                dt_reader = cmd.ExecuteReader();
                while (dt_reader.Read())
                {
                    if (counter % 10 == 0 && counter != 0)
                        src += "</center></div><div class=\"d_tr\"><center>";

                    src += "<div class=\"d_td\">" +
                               "<a href=\"#\" onClick=\"return hs.htmlExpand(this)\">" +
                                   "<img class=\"mybordered\" src=\"" + GlobalVar.image_path + "Temp Cover/" + dt_reader.GetValue(1).ToString() + "\" style=\"margin:0 5px; width:70px; height:90px;\"/>" +
                                "</a>";
                    if (Convert.ToString(dt_reader.GetValue(0)).Length > 18)
                        src += "<div class=\"listscore\"><span>" + Convert.ToString(dt_reader.GetValue(0)).Substring(0, 18) + "...</span></div>";
                    else
                        src += "<div class=\"listscore\"><span>" + Convert.ToString(dt_reader.GetValue(0)) + "</span></div>";

                    src +=          "<div class=\"highslide-maincontent\">" +
                                        "<div class=\"d_table\">" +
                                            "<div class=\"d_tr\">" +
                                                "<div class=\"d_td\"><img src=\"" + GlobalVar.image_path + "Temp Cover/" + Convert.ToString(dt_reader.GetValue(1)) + "\" width=\"150\"/></div>" +
                                                "<div class=\"d_td\" style=\"padding-left:10px; vertical-align:top;\">" +
                                                    "<h4>" + dt_reader.GetValue(0).ToString() + "</h4>" +
                                                    "<p>" + Convert.ToString(dt_reader.GetValue(4)) + "</p>" +
                                                "</div>" +
                                            "</div>" +
                                        "</div>" +
                                    "</div>" +
                                "</div>";
                    counter += 1;
                }
                conn.Close();
                src += "</center></div>';";
                script = src;
                web_Control.ExecuteJavascript(script);
            }
            catch (SqlCeException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        //Main Program
        public MainWindow()
        {
            InitializeComponent();

            //Theme
            MahApps.Metro.ThemeManager.ChangeTheme(this, MahApps.Metro.ThemeManager.DefaultAccents.First(a => a.Name == "Blue"), MahApps.Metro.Theme.Light);

            //Variable
            String _dir = "C:\\Users\\Delta\\Desktop\\anime-code\\branches\\AnimeCode\\Pages\\";
            //String _dir = "C:\\Documents and Settings\\xp\\Desktop\\anime-code\\branches\\AnimeCode\\Pages\\";

            //First Set Up
            web_Control.Source = new Uri(_dir + "index.html");
        }

        private void MetroWindow_ContentRendered(object sender, EventArgs e)
        {
            //Start Here
            if (web_Control.IsDocumentReady == true)
            {
                generateMenuBar("index");
                scoutSetup();
            }

            //Click Happens
            using (JSObject myGlobalObject = web_Control.CreateGlobalJavascriptObject("myGlobalObject"))
            {
                myGlobalObject.Bind("onLinkClicked", false, (sen, eve) =>
                {
                    web_Control.DocumentReady += (se, ev) =>
                    {
                        switch (eve.Arguments[0].ToString())
                        {
                            case "index":
                                generateMenuBar("index");
                                homeSetup();
                                break;

                            case "collection":
                                generateMenuBar("archive");
                                archiveSetup();
                                break;

                            case "watch":
                                generateMenuBar("archive");
                                watchlistSetup();
                                break;

                            case "hof": 
                                generateMenuBar("hof");
                                topSetup();
                                break;

                            case "scout":
                                generateMenuBar("scout");
                                scoutSetup();
                                break;

                            default:
                                generateMenuBar("archive");
                                animeSetup(eve.Arguments[0].ToString());
                                break;
                        }
                    };
                });
            }
        }
    }
}